package org.hong.samples.web;


import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hong.samples.model.VO.ExamScoreVO;
import org.hong.samples.model.VO.ExamSubjectVO;
import org.hong.samples.model.VO.UserJDBCVO;
import org.hong.samples.service.UserStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;

@Controller
@RequestMapping("/student")
public class StudentSubjectController {
	
	@Autowired
	private UserStudentService userStudentService;
	
	@Autowired
	private LocaleResolver localeResolver;
	
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping(value="/subjects", method=RequestMethod.GET)
	public String toSubject(HttpSession session,Model model) throws Exception{
		UserJDBCVO userJDBCVO = (UserJDBCVO) session.getAttribute("examUser");
		List<ExamSubjectVO> examSubjectList = userStudentService.getSubjectList(userJDBCVO);
		model.addAttribute("edit",messageSource.getMessage("page.index.edit", null, LocaleContextHolder.getLocale()));
		model.addAttribute("examSubjectList", examSubjectList);
		return "student/subjects";
	}
	
	@RequestMapping(value="/StudentchangeLocale")
	public String changeLocale(HttpServletRequest request,HttpServletResponse response,String LocaleType){
		if("en_US".equals(LocaleType)){
			localeResolver.setLocale(request, response, Locale.US);
		}else if("zh_CN".equals(LocaleType)){
			localeResolver.setLocale(request, response, Locale.CHINA);
		}
		return "redirect:/student/subjects";
	}
	
	@ResponseBody
	@RequestMapping(value="/showScore",method = RequestMethod.POST)
	public ExamScoreVO showScore(HttpSession session,String scoreId){
		ExamScoreVO vo = userStudentService.getScoreById(scoreId);
		return vo;
	}

}
